package cn.zqh666.jie;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Vibrator;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import java.text.SimpleDateFormat;
import java.util.Date;

public class MainActivity extends AppCompatActivity {

    public static int mCount;
    public static String mParse;
    public static int mDay;
    public static int last_day;
    private TextView count;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @SuppressLint("SetTextI18n")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setStatusBarFullTransparent();
        ImageButton qiandao = findViewById(R.id.qiandao);
        TextView parse = findViewById(R.id.parse);
        if(mParse != null){
            parse.setText(mParse);
        }
        count = findViewById(R.id.conut);
        count.setText("第"+mCount+"天");
        qiandao.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("SimpleDateFormat")
            @Override
            public void onClick(View view) {
                Vibrator vibrator = (Vibrator)getApplication().getSystemService(VIBRATOR_SERVICE);
                assert vibrator != null;
                vibrator.vibrate(500);
                if(mDay == last_day){
                    Toast.makeText(getApplicationContext(), "今天已签到过了，明天再来吧", Toast.LENGTH_SHORT).show();
                    return;
                }
                last_day = mDay;
                mCount++;
                String str = mDay + "," + mCount;
                DocumentTool.writeData(Environment.getExternalStorageDirectory()+"/Jie/data.txt", str);
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日");
                Date date = new Date();
                DocumentTool.writtenFileData(Environment.getExternalStorageDirectory()+"/Jie/log.txt",
                        simpleDateFormat.format(date) + " " + "第" + mCount + "次签到\n");
                count.setText("第"+mCount+"天");
                Toast.makeText(getApplicationContext(), "签到成功，恭喜你离梦想又近一步", Toast.LENGTH_SHORT).show();
            }
        });

    }

    /**
     * 全透状态栏
     */
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    protected void setStatusBarFullTransparent() {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);//显示状态栏
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);

    }

}
